package com.wxg.study.utils;

import com.google.common.base.Strings;
import com.wxg.study.dao.UserInfo;
import org.apache.shiro.crypto.hash.SimpleHash;

public class EncryptionPassword {

    final static String MD5 = "MD5";
    final static int HASH_ITERATIONS = 2;

    /**
     *
     * @param userInfo
     * @param originPwd 需要加密的原始密码,比如 '123456'
     * @return 返回加密后的密文密码
     */
    public static String encryption(UserInfo userInfo, String originPwd) {
        if(null == userInfo || Strings.isNullOrEmpty(originPwd)) {
            return null;
        }

        String getCredentialsSalt = userInfo.getCredentialsSalt();

        SimpleHash simpleHash = new SimpleHash(MD5, originPwd, getCredentialsSalt, HASH_ITERATIONS);

        return simpleHash.toString();
    }

    public static String encryption(String username, String salt, String originPwd) {
        if(Strings.isNullOrEmpty(username)
            || Strings.isNullOrEmpty(salt)
            || Strings.isNullOrEmpty(originPwd)) {
            return null;
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(username);
        userInfo.setSalt(salt);

        return encryption(userInfo, originPwd);
    }

}
